<template>
  <div class="login-bg">
    <div class="login-card">
      <div class="login-title">后台管理系统</div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" :type="passwordVisible ? 'text' : 'password'" placeholder="请输入密码" prefix-icon="Lock" show-password @keyup.enter="handleLogin" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="userStore.loading" style="width: 100%" @click="handleLogin">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="login-footer">
        <span>
          还没有账户？
          <el-link type="primary" @click="goToRegister">立即注册</el-link>
        </span>
        <el-link type="info" @click="goToForgotPassword">忘记密码？</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref(null)
const passwordVisible = ref(false)

const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  await loginFormRef.value.validate()
  const result = await userStore.login(loginForm.value)
  if (result.success) {
    router.push('/dashboard')
  }
}

const goToRegister = () => router.push('/register')
const goToForgotPassword = () => router.push('/forgot-password')
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #e0ffd6 0%, #b2f7ef 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  width: 400px;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.12);
  padding: 40px 36px 32px 36px;
  display: flex;
  flex-direction: column;
  align-items: center;
  animation: fadeIn 0.8s;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(40px);}
  to { opacity: 1; transform: translateY(0);}
}
.login-title {
  font-size: 2rem;
  font-weight: 700;
  color: #222;
  margin-bottom: 32px;
  letter-spacing: 2px;
  text-align: center;
}
.el-form {
  width: 100%;
}
.el-input__wrapper, .el-input {
  border-radius: 8px !important;
}
.el-button {
  border-radius: 8px;
  font-size: 16px;
  height: 44px;
}
.el-button.is-primary {
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
  border: none;
}
.login-footer {
  width: 100%;
  display: flex;
  justify-content: space-between;
  margin-top: 18px;
  font-size: 15px;
}
.login-footer .el-link {
  font-size: 15px;
}
@media (max-width: 600px) {
  .login-card {
    width: 95vw;
    padding: 24px 8px 16px 8px;
  }
  .login-title {
    font-size: 1.3rem;
  }
}
</style>
